# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=bullet
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=2.83.7
pkgrel=1
pkgdesc="A 3D Collision Detection and Rigid Body Dynamics Library for games and  animation (mingw-w64)"
arch=('any')
url="http://www.bulletphysics.com/Bullet/"
license=('custom:zlib')
options=('!strip' 'staticlibs')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-freeglut")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/bulletphysics/bullet3/archive/${pkgver}.tar.gz"
        cast-error.patch
        promit-bulletcollisions.patch)
sha256sums=('00d1d8f206ee85ffd171643ac8e72f9f4e0bf6dbf3d4ac55f4495cb168b51243'
            '9f7901cc040c00e94763ae36e886a5ed98faec169b17aac2d85c64eff45bc17f'
            'a91c28949d0e1535caa4feb05973e67213bf055887f6e465a54c8b0b1fcb11cd')

prepare () {
  cd ${srcdir}/bullet3-${pkgver}
  patch -p1 -i ${srcdir}/cast-error.patch
  patch -p0 -i ${srcdir}/promit-bulletcollisions.patch
}

build() {
  plain "No step"
}

build_release() {
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-freeglut")
  install=${_realname}-${CARCH}.install

  [[ -d build-release ]] && rm -rf build-release
  mkdir -p build-release && pushd build-release
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}\
    -DBUILD_SHARED_LIBS=1 \
    -DBUILD_EXTRAS=1 \
    -DINSTALL_LIBS=1 \
    -DUSE_GLUT=1 \
    -DCMAKE_CXX_FLAGS_RELEASE="-fpermissive" \
    -DINSTALL_EXTRA_LIBS=1 \
    -DCMAKE_BUILD_TYPE=Release \
    ../bullet3-${pkgver}

  make
  make DESTDIR=${pkgdir} install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/bullet
  for f in $(find examples -type f -iname *.dll -o -iname *.exe); do
    cp ${f} ${pkgdir}${MINGW_PREFIX}/share/bullet/
  done
  cp -rf ${srcdir}/bullet3-${pkgver}/data/* ${pkgdir}${MINGW_PREFIX}/share/bullet/
  for f in gtest gwen OpenGLWindow; do
    cp lib/lib${f}.dll ${pkgdir}${MINGW_PREFIX}/share/bullet/
  done

  mv -f ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin/
}

build_debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}")

  [[ -d build-debug ]] && rm -rf build-debug
  mkdir -p build-debug && pushd build-debug
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}  \
    -DBUILD_SHARED_LIBS=1 \
    -DBUILD_EXTRAS=1 \
    -DINSTALL_LIBS=1 \
    -DUSE_GLUT=1 \
    -DCMAKE_CXX_FLAGS_RELEASE="-fpermissive" \
    -DINSTALL_EXTRA_LIBS=1 \
    -DCMAKE_BUILD_TYPE=Debug \
    -DCMAKE_DEBUG_POSTFIX="_d" \
    ../bullet3-${pkgver}

  make
  make DESTDIR=${pkgdir} install

  mv -f ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin/

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/cmake
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig

}

package_mingw-w64-i686-bullet() {
  build_release
}

package_mingw-w64-i686-bullet-debug() {
  build_debug
}

package_mingw-w64-x86_64-bullet() {
  build_release
}

package_mingw-w64-x86_64-bullet-debug() {
  build_debug
}
